package com.scalar.chiptrack.sales.actions;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.sales.forms.ContactPersonAddressBookForm;
import com.scalar.chiptrack.sales.dao.CustomerDAO;
import com.scalar.chiptrack.sales.ContactPerson;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.dao.UserDAO;

import java.util.ArrayList;
import java.util.HashMap;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by IntelliJ IDEA.
 * User: prasad
 * Date: Jul 25, 2007
 */
public class ContactPersonAddressBookAction extends ChipTrackAction
{
	private static final String ACTION_NAME = "/contactPersonAddressBook.do" ;

    public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception
    {

        String forward          = "success";
        String action           = request.getParameter("action");
        String customerIdStr    = request.getParameter("customerId");
        String salesOrderIdStr  = request.getParameter( "salesOrderId" );
        String quotationIdStr   = request.getParameter( "quotationId" );
        String closeWindowAfterSave = request.getParameter( "closeWindowAfterSave" );
        String showAgain        = request.getParameter( "showAgain" );
        String sourceType       = request.getParameter( "sourceType" );

        int customerId;
        int salesOrderId;
        int quotationId;

        HashMap info            = new HashMap ();
        HashMap errors          = new HashMap ();

        HttpSession mSession    = request.getSession();
        User user               = ( User ) mSession.getAttribute ( SessionKeys.USER_KEY );

        request.setAttribute ( SessionKeys.INFO_MESSAGES, info );
        request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );

        ContactPersonAddressBookForm addressBookForm = (ContactPersonAddressBookForm) form;

        addressBookForm.setSourceType( sourceType );

        if( sourceType == null || sourceType.trim().length() == 0 )
        {
            sourceType = "SalesOrder";
        }

        if ( user == null )
        {
            mSession.setAttribute ( SessionKeys.SOURCE_PAGE_KEY,  ACTION_NAME );
            return ( mapping.findForward ( "login" ) );
        }

        try
        {
            customerId = Integer.parseInt( customerIdStr );
        }catch( NumberFormatException nfe ){ customerId = -1; }

        try
        {
            salesOrderId = Integer.parseInt( salesOrderIdStr );
        }catch( NumberFormatException nfe ){ salesOrderId = -1; }

        try
        {
            quotationId = Integer.parseInt( quotationIdStr );
        }catch( NumberFormatException nfe ){ quotationId = -1; }

        if( "showContactPersons".equalsIgnoreCase(action) )
        {
            /*if( showAgain != null && "true".equalsIgnoreCase( showAgain ) )
            {
                ArrayList quotationContactPersons   = addressBookForm.getContactPersonsList();
                for ( int i = 0; i < quotationContactPersons.size (); i++ )
                {
                    ContactPerson contactPerson = ( ContactPerson ) quotationContactPersons.get ( i );
                    System.out.println("Action=showContactPersons = " + contactPerson.toString() );
                }
            }*/
            if( customerId > 0 && ( showAgain == null || "false".equalsIgnoreCase( showAgain ) || "null".equalsIgnoreCase( showAgain ) || "".equalsIgnoreCase( showAgain ) ) )
            {
                ArrayList contactPersonList         = null;
                ArrayList internalApproversEmails   = UserDAO.loadAllApproversEmailsList( null, -1, addressBookForm.getSourceType() );

                addressBookForm.setCustomerId( customerId );
                addressBookForm.setSalesOrderId( salesOrderId );
                addressBookForm.setQuotationId( quotationId );

                contactPersonList = CustomerDAO.loadCustomerAllContactPersonsDetails( null, customerId );
                if( contactPersonList == null )
                {
                    contactPersonList = new ArrayList();
                }
                contactPersonList.addAll( internalApproversEmails );

                addressBookForm.setContactPersonsList( contactPersonList );

                showAgain = "true";
            }            
        }
        else if( "saveContactPersons".equalsIgnoreCase(action) )
        {
            if( closeWindowAfterSave != null && "true".equalsIgnoreCase( closeWindowAfterSave ) )
            {
                String[] selectedValues = ( String[] ) request.getParameterValues( "contactPersonSelected" );
                ArrayList quotationContactPersons = addressBookForm.getContactPersonsList();

                for ( int i = 0; i < quotationContactPersons.size (); i++ )
                {
                    ContactPerson contactPerson = ( ContactPerson ) quotationContactPersons.get ( i );
                    if( selectedValues[i] != null && selectedValues[i].trim().length() > 0 && "true".equalsIgnoreCase( selectedValues[i] ) )
                    {
                        contactPerson.setIsChecked( true );    
                    }
                    else
                    {
                        contactPerson.setIsChecked( false );
                    }
                }

                request.setAttribute( "closeWindowAfterSave", "true" );
            }
        }

        request.setAttribute( "showAgain", showAgain );

        return ( mapping.findForward( "success" ) );
    }
}
